package cn.sunlight.mm.wx.controller;

import cn.sunlight.framework.annotation.HmComponent;
import cn.sunlight.framework.annotation.HmRequestMapping;
import cn.sunlight.framework.annotation.HmSetter;
import cn.sunlight.mm.base.BaseController;
import cn.sunlight.mm.entity.Result;
import cn.sunlight.mm.pojo.WxMember;
import cn.sunlight.mm.wx.service.CategoryService;
import cn.sunlight.mm.wx.service.WxMemberService;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sunlight
 * @description: 题目分类控制器
 * @date 2021/6/1
 * @Version v1.0
 */
@Slf4j
@HmComponent
public class CategoryController extends BaseController {

    @HmSetter("memberService")
    private WxMemberService memberService;

    @HmSetter("categoryService")
    private CategoryService categoryService;

    /**
     * 获取题目详情
     * @param request
     * @param response
     * @throws Exception
     */
    @HmRequestMapping("/category/question/list")
    public void getCatagoryQuestionList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            //获取前端参数数据
            Map<String,Object> mapData = parseJSON2Object(request, HashMap.class);
            //根据openId获取会员对象
            String openId = getHeaderAuthorization(request);
            WxMember wxMember=memberService.findWxMemberByOpenId(openId);
            //把会员ID、学科ID、城市ID设置参数容器
            mapData.put("memberId",wxMember.getId());
            mapData.put("courseId",wxMember.getCourseId());
            mapData.put("cityId",wxMember.getCityId());
            log.debug("调用Service，获取分类基本信息及分类下的题目列表，参数:{}",mapData);
            Map<String,Object> resultMap = categoryService.findCatagoryQuestionList(mapData);

            printResult(response,new Result(true,"获取成功",resultMap));
        }catch (RuntimeException e){
            e.printStackTrace();
            log.error("",e);
            printResult(response,new Result(false,"获取失败"+e.getMessage()));
        }
    }

    /**
     * 题目分类列表
     * @param request
     * @param response
     * @throws Exception
     */
    @HmRequestMapping("/category/list")
    public void getCategoryList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            //获取请求数据
            Map<String,Object> mapData=parseJSON2Object(request, HashMap.class);
            log.debug("map:{}",mapData);

            //从header中获取OpenId，并放入客户端参数容器
            String openId = getHeaderAuthorization(request);
            mapData.put("openId",openId);

            //根据openId获取会员，同时获取memberId,cityId,courseId
            WxMember wxMember = memberService.findWxMemberByOpenId(openId);
            mapData.put("memberId",wxMember.getId());
            mapData.put("courseId",wxMember.getCourseId());
            mapData.put("cityId",wxMember.getCityId());
            log.debug("调用Service-mapData:{}",mapData);

            //调用service获取题目分类列表
            List<Map> mapList=categoryService.findCategoryList(mapData);

            printResult(response,new Result(true,"题目分类列表成功",mapList));
        }catch (RuntimeException e){
            e.printStackTrace();
            log.error("",e);
            printResult(response,new Result(false,"题目分类列表失败"+e.getMessage()));
        }
    }

}
